package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.support.imp.IItemSelectListener

import com.youdianstar.app.entity.CouponInfo
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.MathUtil
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.databinding.LayoutItemCouponBinding
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.ex.parseTx
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.util.DataHelperEx


/**
 * File: RedeemPagingAdapter
 * Author: 82149 Create: 2022/2/28 18:16
 * Changes (from 2022/2/28)
 * --------------------------------------------------
 * description: 可兑换优惠券列表
 * ---------------------------------------------------
 *
 */
class RedeemPagingAdapter(val mContext: Context, val type: Int, val listener: IItemSelectListener) : PagingDataAdapter<CouponInfo, RedeemPagingAdapter.ListHolder>(differCallback) {
    companion object {
        val TAG = "RedeemPagingAdapter"
        val differCallback = object : DiffUtil.ItemCallback<CouponInfo>() {
            override fun areItemsTheSame(oldItem: CouponInfo, newItem: CouponInfo): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: CouponInfo, newItem: CouponInfo): Boolean {
                return oldItem == newItem
            }
        }
    }
    var languageNow :String = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
    }

    //积分账户
    private var pointBalance:Int = 0

    fun setPointBalance(balance:Int){
        pointBalance = balance
    }

    class ListHolder(val binding : LayoutItemCouponBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        getItem(position)?.let { item ->
            //抵扣类型:1-免费时长 2-优惠金额 3-金额折扣
            val symbolStr = DataAppManager.instance.symbolTx()
            val deductTitle = if (null!=item.deduction) DataHelperEx.couponDeductTx(mContext,symbolStr,item.deduction) else "--"
            holder.binding.tvCouponName.text = deductTitle
            holder.binding.tvPoints.text = "${item.exchangePoint} "+mContext.getString(R.string.coupon_pts)
            //兑换进度
            val processFt = MathUtil.mul(item.redeemedRate,100.0F)
            holder.binding.barRedeem.progress = processFt.toInt()
            if (item.redeemedRate==0F){
                holder.binding.tvRedeemProcess.text = "${mContext.getString(R.string.coupon_redeemed)}: 0%"
            }else
                holder.binding.tvRedeemProcess.text = "${mContext.getString(R.string.coupon_redeemed)}: ${processFt.toInt()}%"

            //holder.binding.btnRedeemS.isEnabled = item.redeemedRate<1.0F
            //加个积分不足判断
            if (pointBalance >= item.exchangePoint){
                holder.binding.btnRedeemS.text = mContext.getString(R.string.coupon_redeemNow)
                holder.binding.btnRedeemS.visibility = View.VISIBLE
                holder.binding.tvAccountState.visibility = View.INVISIBLE
            }else{
                holder.binding.btnRedeemS.visibility = View.INVISIBLE
                holder.binding.tvAccountState.visibility = View.VISIBLE
            }


            //1. 使用说明
            var desTx = "--"
            item.description?.let { des ->
                desTx = when(languageNow){
                    Language.ES -> des.es.parseTx()
                    Language.ZH -> des.zh.parseTx()
                    else -> des.en.parseTx()
                }
            }
            holder.binding.tvDesContent.text = desTx
            //2. 可用商家; 0-通用优惠券 1-商家优惠券 ;适配用
            if (StrUtil.isEmpty(item.merchantNameList)){
                holder.binding.tvUseContent.text = mContext.getString(R.string.coupon_allStationUse)
            }else{
                holder.binding.tvUseContent.text = item.merchantNameList
            }
            holder.binding.ivEpDex.rotation = if (item.isExp) 180F else 0F
            holder.binding.clCouponPart2.visibility = if (item.isExp) View.VISIBLE else View.GONE
        }

        holder.binding.ivEpDex.setThrottleListener {
            onItemViewExpAction(holder.bindingAdapterPosition)
        }

        holder.binding.txEpDes.setThrottleListener {
            onItemViewExpAction(holder.bindingAdapterPosition)
        }

        holder.binding.btnRedeemS.setThrottleListener {
            getItem(holder.bindingAdapterPosition)?.let { info ->
                listener.onSelectItem(holder.bindingAdapterPosition,type, info)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemCouponBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    /**更新布局展开状态*/
    private fun updateItemViewExp(position: Int, expAction:Boolean){
        getItem(position)?.isExp = expAction
        notifyItemChanged(position)
    }

    private fun onItemViewExpAction(position: Int){
        getItem(position)?.let { info ->
            val isExpNow = info.isExp
            updateItemViewExp(position, !isExpNow)
        }
    }
}